$(document).ready(function() {
	var ChannelId=getItems("Channel") != null ? getItems("Channel") : "mychannel";
	var FabricFlag=getItems("networkName");
	var Token=getItems("token");

	if(loginToken){
		$(".Reminder").hide();
		if(getUrlParam('TransactionId') != null) {
			//查询单次交易
			var queryTransactionUrl = '/api/block/public/queryTransaction';
			var queryTransactionoData = {
				"RequestType": "QueryTransactionReq",
				"ChannelId":ChannelId==null?"mychannel":ChannelId,
				"FabricFlag":FabricFlag,
				"QueryTransactionArgs": {
					"TransactionId": getUrlParam('TransactionId')
				}
			};
			
			$(".cover").show();
			requestPost(queryTransactionUrl, queryTransactionoData, 0);
		} else {
			//查询最新一个的交易
			var queryChannelInfoUrl = '/api/block/public/queryChannelInfo';
			var queryChannelInfoData = {
				"RequestType": "QueryChannelInfoReq",
				"ChannelId":ChannelId==null?"mychannel":ChannelId,
				"FabricFlag":FabricFlag
			};
			$(".cover").show();
			var queryBlockUrl='/api/block/public/queryBlock';
			queryChannelInfo(queryChannelInfoUrl, queryChannelInfoData,queryBlockUrl,"QueryBlockByNumReq");
	
		}
	}else{
		$(".Reminder").show();
		if(getUrlParam('TransactionId') != null) {
			//查询单次交易
			var queryTransactionUrl = '/api/block/cache/queryTransaction';
			var queryTransactionoData = {
				"RequestType": "QueryTransactionReq",
				"ChannelId":ChannelId==null?"mychannel":ChannelId,
				"FabricFlag":FabricFlag,
				"QueryTransactionArgs": {
					"TransactionId": getUrlParam('TransactionId')
				}
			};
			$(".cover").show();
			requestPost(queryTransactionUrl, queryTransactionoData, 0);
		} else {
			//查询最新一个的交易
			var queryChannelInfoUrl = '/api/block/cache/queryChannelInfo';
			var queryChannelInfoData = {
				"RequestType": "QueryChannelInfoReq",
				"ChannelId":ChannelId==null?"mychannel":ChannelId,
				"FabricFlag":FabricFlag,
			};
			$(".cover").show();
			var queryBlockUrl='/api/block/cache/queryBlock';
			queryChannelInfo(queryChannelInfoUrl, queryChannelInfoData,queryBlockUrl,"QueryNewBlock");

		}

	}

	function requestPost(url, Data, num) {
		var oData = Data;
		$.ajax({
			type: "POST",
			contentType: "application/json",
			url: url,
			data: JSON.stringify(oData),
			headers:{
				Authorization:Token,
			},
			dataType: "json",
			success: function(data) {
				$(".cover").hide() 
				switch(num) {
					case 0:
						if(data.code==200){
							if(data.status==40101){
								$(".containerBox").html('<div class="NoData">登录已失效，请重新登录！</div>');
								$(".NoData").show();
								$(".NoData").css({
									marginTop:30
								})
							}else{
								var oData=JSON.parse(data.data);
								// console.log(oData);
								appendStr2(oData)
							}
							
						}else{
							$(".containerBox").html('<div class="NoData">暂无查询到此数据</div>');
							$(".NoData").show();
							$(".NoData").css({
								marginTop:30
							})
						}
						break;
					case 1:
						if(data.code==200){
							if(data.status==40101){
								$(".containerBox").html('<div class="NoData">登录已失效，请重新登录！</div>');
								$(".NoData").show();
								$(".NoData").css({
									marginTop:30
								})
							}else{
								// console.log(data);
								var oData=JSON.parse(data.data);
								appendStr(oData);
							}
						}else{
							$(".containerBox").html('<div class="NoData">暂无查询到此数据</div>');
							$(".NoData").show();
							$(".NoData").css({
								marginTop:30
							})
						}	
						break;
					default:
						console.log('------');
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$(".cover").hide();
				$(".containerBox").html('<div class="NoData">暂无数据</div>');
				$(".NoData").show();
				$(".NoData").css({
					marginTop:30
				})
				console.log("调取sns接口出错");
				console.log(XMLHttpRequest.status);
				console.log(XMLHttpRequest.readyState);
				console.log(textStatus);
			}
		});
	}

	function queryChannelInfo(url, Data,uri,parame) {
		var oData = Data;
		$.ajax({
			type: "POST",
			contentType: "application/json",
			url: url,
			data: JSON.stringify(oData),
			headers:{
				Authorization:Token,
			},
			dataType: "json",
			success: function(data) {
				$(".cover").hide(); 
				if(data.code==200){
					if(data.status==40101){
						$(".containerBox").html('<div class="NoData">登录已失效，请重新登录！</div>');
						$(".NoData").show();
						$(".NoData").css({
							marginTop:30
						})
					}else{
						var oData=JSON.parse(data.data);
		
						var queryBlockUrl = uri;
						var queryBlockoData = {
							"RequestType": parame,
							"ChannelId":ChannelId==null?"mychannel":ChannelId,
							"FabricFlag":FabricFlag,
							"QueryBlockInfo": {
								"QueryType": 1,
								"StartIndex": parseInt((oData.ChannelInfo.Heigth-1)),
								"Count": 1
							}
						}
						requestPost(queryBlockUrl, queryBlockoData, 1);
					}
					
				}else{
					$(".containerBox").html('<div class="NoData">暂无数据</div>');
					$(".NoData").show();
					$(".NoData").css({
						marginTop:30
					})
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$(".cover").hide();
				$(".containerBox").html('<div class="NoData">暂无数据</div>');
				$(".NoData").show();
				$(".NoData").css({
					marginTop:30
				})
				console.log("调取sns接口出错");
				console.log(XMLHttpRequest.status);
				console.log(XMLHttpRequest.readyState);
				console.log(textStatus);
			}
		});
	}
	
	function timestampToTime(timestamp) {
		var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        Y = date.getFullYear() + '-';
        M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        D = date.getDate() + ' ';
        h = date.getHours()<10?"0"+date.getHours()+":":date.getHours() + ':';
        m = date.getMinutes()<10?"0"+date.getMinutes()+":":date.getMinutes() + ':';
        s = date.getSeconds()<10?"0"+date.getSeconds():date.getSeconds();
        return Y+M+D+h+m+s;
   }
	
   //最新交易
	function appendStr(oData){
//		console.log(oData.BlockInfoList[0].TransactionList[0].TransactionId)
		if(oData.BlockInfoList[0].TransactionList[0].TransactionType==3){
			var str='<div class="transactionTop"><div class="row"><div class="col-xs-8"><div class="transactionName"><span>交易哈希</span><a  onclick="DataDetails()">'+oData.BlockInfoList[0].TransactionList[0].TransactionId+'</a></div></div><div class="col-xs-4"><div class="transactionTime"><span>时间戳</span><a>'+timestampToTime(oData.BlockInfoList[0].TransactionList[0].TransactionTime)+'</a></div></div></div></div>';
			$(".transactionBox").html(str);
			//设置localStorage+sessionStorage
			var TransactionData=JSON.stringify(oData);
			setItems("TransactionData",TransactionData);
		}else{
			$(".containerBox").html('<div class="NoData">暂无数据</div>');
			$(".NoData").show();
			$(".NoData").css({
				marginTop:30
			})
		}
	}
	
	//单次交易
		function appendStr2(oData){
		if(oData.TransactionInfo.TransactionType==3){
			var str='<div class="transactionTop"><div class="row"><div class="col-xs-8"><div class="transactionName"><span>交易哈希</span><a  onclick="DataDetails2()">'+oData.TransactionInfo.TransactionId+'</a></div></div><div class="col-xs-4"><div class="transactionTime"><span>时间戳</span><a>'+timestampToTime(oData.TransactionInfo.TransactionTime)+'</a></div></div></div></div>';
			$(".transactionBox").html(str);
			//设置localStorage+sessionStorage
			var TransactionData=JSON.stringify(oData);
			setItems("TransactionData",TransactionData);
		}
	}
	
	
	DataDetails=function (data){  
		var dataValue=JSON.parse(getItems("TransactionData"));

		var str='<p><span>频道：</span><a>'+dataValue.BlockInfoList[0].TransactionList[0].ChannelId+'</a></p><p><span>组织：</span><a>'+dataValue.BlockInfoList[0].TransactionList[0].Organization+'</a></p><p><span>交易哈希：</span><a>'+dataValue.BlockInfoList[0].TransactionList[0].TransactionId+'</a></p><p><span>交易时间：</span><a>'+timestampToTime(dataValue.BlockInfoList[0].TransactionList[0].TransactionTime)+'</a></p><p><span>用户：</span><a>'+dataValue.BlockInfoList[0].TransactionList[0].User+'</a></p>'
        $(".transactionDialog").show();
		$(".transactionHtml").html(str);
    }  
    
    DataDetails2=function (data){  
		var dataValue=JSON.parse(getItems("TransactionData"));

		var str='<p><span>频道：</span><a>'+dataValue.TransactionInfo.ChannelId+'</a></p><p><span>组织：</span><a>'+dataValue.TransactionInfo.Organization+'</a></p><p><span>交易哈希：</span><a>'+dataValue.TransactionInfo.TransactionId+'</a></p><p><span>交易时间：</span><a>'+timestampToTime(dataValue.TransactionInfo.TransactionTime)+'</a></p><p><span>用户：</span><a>'+dataValue.TransactionInfo.User+'</a></p>'
        $(".transactionDialog").show();
		$(".transactionHtml").html(str);
    }  
    
    $(".close").click(function(){
    	$(".transactionDialog").hide();
    })
    
    $(".transactionDialog").click(function(){
    	$(this).hide();
    })
})